package com.example.utils;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Result {

    public static final int SUCCESS = 200;

    public static final int NOT_FOUND = 404;

    public static final int NO_AUTH = 403;

    public static final int NO_LOGIN = 403;

    public static final int ERROR = 500;

    private int code;

    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /***
     *
     * 操作成功
     * @return
     */
    public static Result success() {
        Result result = new Result();
        result.setCode(SUCCESS);
        return result;
    }

    /****
     *
     * 操作成功
     * @param data
     * @return
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setData(data);
        return result;
    }

    /****
     * 操作失败
     * @return
     */
    public static Result error() {
        Result result = new Result();
        result.setCode(ERROR);
        return result;
    }

    /****
     * 操作失败
     * @param data
     * @return
     */
    public static Result error(Object data) {
        Result result = new Result();
        result.setCode(ERROR);
        result.setData(data);
        return result;
    }

    /****
     *
     * 没有权限
     * @return
     */
    public static Result noAuth() {
        Result result = new Result();
        result.setCode(NO_AUTH);
        result.setData("没有权限");
        return result;
    }


    /****
     * 用户还未登录
     * @param response
     * @return
     */
    public static void noLogin(ServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write("还未登录，请登陆后再访问.");
        printWriter.flush();
    }
}
